﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
namespace GiaoDien
{
    public partial class FORMTHELOAISACH : Form
    {
        private DataTable datatable;
        private string sqlquery;
        public FORMTHELOAISACH()
        {
            InitializeComponent();
        }

        private void btnlapthe_Click(object sender, EventArgs e)
        {
            if (txbmatheloai.Text != "" && txbtentheloai.Text != "")
            {
                THELOAISACHDTO tlsDTO = new THELOAISACHDTO();
                tlsDTO.Maloaisach = txbmatheloai.Text.Trim().ToUpper();
                tlsDTO.Tentheloai = txbtentheloai.Text.Trim().ToUpper();
                THELOAISACHBUS tlsBUS = new THELOAISACHBUS();
                int kq = tlsBUS.Themtheloai(tlsDTO);
                if (kq > 0)
                {
                    MessageBox.Show("thêm thành công");
                    xemtatca();
                }
                else if (kq == 0)
                    MessageBox.Show("thêm thất bại");
                else if (kq == -1)
                    MessageBox.Show("mã hoặc tên bộ phận đã có trong csdl");
            }
            else
                MessageBox.Show("thiếu thông tin");
            
        }

        private void init()
        {
            datatable = new DataTable();
            sqlquery = "SELECT * FROM THE_LOAI_SACH";
        }

        private void xemtatca()
        {
            THELOAISACHBUS tblBUS = new THELOAISACHBUS();
            datatable = tblBUS.Loadtheloaisach();
            this.dataGridView1.DataSource = datatable;
        }

        private void FORMTHELOAISACH_Load(object sender, EventArgs e)
        {
            init();
            xemtatca();
        }

        private void btmxemtatca_Click(object sender, EventArgs e)
        {
            xemtatca();
        }

        private void tracuuma(string ma)
        {
            DataView dataview = new DataView(datatable);
            string filter = "MaLoaiSach = '" + ma + "'";
            dataview.RowFilter = filter;
            this.dataGridView1.DataSource = dataview;
        }

        private void tracuuten(string ten)
        {
            DataView dataview = new DataView(datatable);
            string filter = "TenTheLoai = '" + ten + "'";
            dataview.RowFilter = filter;
            this.dataGridView1.DataSource = dataview;
        }

        private void btntracuu_Click(object sender, EventArgs e)
        {
            if (txbmatheloai2.Text != "")
                tracuuma(txbmatheloai2.Text.Trim().ToUpper());
            else if(txbtentheloai2.Text!="")
                tracuuten(txbtentheloai2.Text.Trim().ToUpper());
        }

        private void capnhat()
        {
            sqlquery = "SELECT * FROM THE_LOAI_SACH";
            THELOAISACHBUS tlsBUS = new THELOAISACHBUS();
            int kq = tlsBUS.update(sqlquery, datatable);
            if (kq > 0)
                MessageBox.Show("Thành công");
            else
                MessageBox.Show("Thất bại");
        }

        private void btncapnhat_Click(object sender, EventArgs e)
        {
            capnhat();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                DialogResult kq = MessageBox.Show("bạn chắc không?", "nguyen", MessageBoxButtons.OKCancel);
                if (kq == DialogResult.OK)
                {
                    datatable.AcceptChanges();
                    capnhat();
                }
                else
                {
                    datatable.RejectChanges();
                }
            }
            
        }
    }
}
